package com.cgbl.service.ui

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.assist.ProjectAssist
import com.cgbl.service.databinding.AppActivityMemberFrameProfileBinding
import com.cgbl.service.pop.CommonConfirmPop
import com.cgbl.service.pop.SelectJobStatusPop
import com.cgbl.service.vm.MemberFrameProfileViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean
import com.robot.base.util.arouter.ARouterUtil

@Route(path = AppConstants.Router.Project.A_MEMBER_FRAME_PROFILE)
class MemberFrameProfileActivity :
    GLBaseActivity<AppActivityMemberFrameProfileBinding, MemberFrameProfileViewModel>() {

    private lateinit var member: MemberAdapterBean

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_member_frame_profile

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        viewModel.setMember(member)
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "详情"
        backView?.setOnClickListener { finish() }
    }

    override fun initViewObservable() {
        viewModel.uc.showSelectJobStatusEvent.observe(this) {
            val selectJobStatusPop = SelectJobStatusPop(this@MemberFrameProfileActivity) { status ->
                member.onlineStatus = status.toString()
                viewModel.setMember(member)
            }
            XPopup.Builder(this@MemberFrameProfileActivity).asCustom(selectJobStatusPop).show()
        }

        viewModel.uc.showDeleteProfileEvent.observe(this) {
            val commonConfirmPop = CommonConfirmPop(this@MemberFrameProfileActivity)
            XPopup.Builder(this@MemberFrameProfileActivity).asCustom(commonConfirmPop).show()
            commonConfirmPop.setTitle("确认删除该条信息吗?").setLeftText("取消")
                .setRightText("确定").setDesc("删除信息工单内将不存在该条消息")
                .setRightClickListener {
                    viewModel.delete()
                }
        }
    }

    override fun initParam() {
        val memberAdapter =
            intent.getSerializableExtra(AppConstants.BundleKey.MEMBER_BEAN) as MemberAdapterBean?
        if (memberAdapter == null || TextUtils.isEmpty(ProjectAssist.getCurrentProjectId())) finish()
        member = memberAdapter!!
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == AppConstants.ActivityRequestCode.SET_WARD_RESULT) {
                val reward = data?.getStringExtra(AppConstants.BundleKey.REWARD_MONEY)
                member.wages = reward
                viewModel.setMember(member)
            } else if (requestCode == AppConstants.ActivityRequestCode.SET_JOB_RESULT) {
                val job = data?.getStringExtra(AppConstants.BundleKey.MEMBER_JOB)
                member.career = job
                viewModel.setMember(member)
            }
        }
    }

}